﻿module Program

open System
open Funogram.Bot
open Funogram.Api
open ExtCore.Control
open Types
open Tools

/// Handler for '/hello'.
let onHello context =
  maybe {
    let! message = context.Update.Message
    let! name = message.Chat.FirstName
    let! username = message.Chat.Username
    sprintf "Hello, %s (%s)!" name username
    |> sendMessage message.Chat.Id
    |> api context.Config
    |> Async.Ignore
    |> Async.Start
  } |> ignore

/// Handles all Telegram events.
let update settings context =
  processCommands context
    [ cmd "/hello" onHello ]
  |> ignore

[<EntryPoint>]
let main _ =
  deserialize<Config> "config.json"
  |> Option.map (fun config ->
    let settings = { Logger = { Log = Console.WriteLine }; Config = config }
    startBot {
      defaultConfig with
        Token = settings.Config.TgToken
    } (update settings) None |> Async.RunSynchronously)
  |> ignore
  0
